import { PRIORITY_VIEW } from "../../../Core/CoreDefine";
import { GCtrl } from "../../../Core/GCtrl";
import GChild from "../../../Core/GView/GChild";
import GPage from "../../../Core/GView/GPage";
import GViewBase from "../../../Core/GView/GViewBase";
import MathEx from "../../../Core/Math/MathEx";
import GameMgr from "../../Logic/GameMgr";
import { CMsg, PART_BTL_SCENE } from './../../Common/Define';
import { Res } from './../../Common/UIResources';
import LevelItem from "./LevelItem";

const { ccclass, property, executionOrder, menu } = cc._decorator;
@ccclass @menu('View/Home/LevelCtrl')
export default class LevelCtrl extends GViewBase {  //GViewBase
	@property(cc.Node) arrowNode: cc.Node = null;
	@property(cc.Node) lvNode: cc.Node = null;
	@property(cc.ScrollView) lvView: cc.ScrollView = null;

	private _curSection: number = 0;
	public onGLoad(): void {
		GCtrl.ES.on(CMsg.client.fight_ui.onFinishSettle, this, this.onFinishSettle.bind(this), PRIORITY_VIEW);
	}

	public onGStart(): void {
		this.assetImpl.prefab(Res.prefab.vw.home.levelItem, () => {
			this.initLevelLayer(true);
		})
		GCtrl.ES.on(CMsg.client.test.export, this, this.exprotInfo.bind(this), PRIORITY_VIEW)
	}

	protected exprotInfo() {
		this.lvNode.children.forEach(node => {
			let comp = node.getComponent(LevelItem);
			console.info(`关卡id:${comp.levelData.id},关卡所在位置:[${node.position.x},${node.position.y}]`)
		})
	}

	protected onFinishSettle(_, result: { sceneId: number, isWin: boolean }) {
		if (result.sceneId != PART_BTL_SCENE.GUANQIA) return;
		let curRaw = GameMgr.userMgr.getCurLevelRaw();
		if (curRaw.section != this._curSection) {
			this.initLevelLayer(true);
		} else {
			let curId = curRaw.id;
			let last = curId - 1;
			this._changeLevelItemState(last, curId);
		}
	}

	/** 关卡通关效果展示 */
	protected _changeLevelItemState(lastFubenId: number, curFubenId: number) {
		let lastLvItem = this.lvNode.getChildComByName("ITEM_" + lastFubenId, LevelItem);
		if (lastLvItem) lastLvItem.changeItemState();
		let curLvItem = this.lvNode.getChildComByName("ITEM_" + (lastFubenId + 1), LevelItem);
		if (curLvItem) {
			curLvItem.changeItemState();
			curLvItem.playIntoAni(() => {
				lastFubenId++;
				if (lastFubenId < curFubenId) {
					this._changeLevelItemState(lastFubenId, curFubenId);
				}
			})
		}
		if (lastLvItem) {
			let socroll = this.lvView;
			let halfHeight = cc.winSize.height / 2;
			if (lastLvItem.node.y > halfHeight) {
				socroll.scrollToOffset(cc.v2(0, - lastLvItem.node.position.y), 1);
			}
		}
	}

	protected initLevelLayer(isAction: boolean) {
		if (!this.lvNode) return;
		let curRaw = GameMgr.userMgr.getCurLevelRaw();
		if (!curRaw) return;
		this._curSection = curRaw.section;
		let raws = GameMgr.storyData.getStorysBySection(curRaw.section);
		if (!raws) return;
		this.arrowNode.destroyAllChildren();
		let maxY = 0;
		for (let i = 0; i < raws.length; i++) {
			let raw = raws[i];
			let node: cc.Node, lvItem: LevelItem = null;
			node = this.lvNode.children[i];
			if (!node) {
				lvItem = this.addGChild<LevelItem>(Res.prefab.vw.home.levelItem, this.lvNode);
				node = lvItem.node;
			} else {
				lvItem = node.getComponent(LevelItem);
			}
			node.position = cc.v2(raw.pointXY[0], raw.pointXY[1])
			if (maxY <= node.position.y) {
				maxY = node.position.y;
			}
			node.opacity = 0;
			let points: cc.Node[] = [];
			if (i != 0) {
				let from: cc.Vec2 = this.lvNode.children[i - 1].position;
				let to: cc.Vec2 = this.lvNode.children[i].position;
				points = this._creatorArrow(from, to, curRaw.id >= raw.id);
			}
			lvItem.init(raw, i, points, raws.length, this.lvView)
		}
		if (true) {
			this.lvNode.parent.height = maxY + 240;
		}

		for (let i = this.lvNode.children.length - 1; i >= raws.length; i--) {
			this.lvNode.children[i].destroy();
		}
		this._onDisplayShowLevelItems(isAction);
	}


	/** 获取路径节点 */
	protected _creatorArrow(from: cc.Vec2, to: cc.Vec2, finish: boolean) {
		let points = []
		let pointsNode: cc.Node[] = [];
		let dis = MathEx.getDistance(from, to) + 25;
		let num = Math.floor(dis / 25);
		for (let i = 1; i < num - 1; i++) {
			let result = from.lerp(to, (i + 0.5) / num);
			points.push(result);
		}
		for (let i = 0; i < points.length; i++) {
			var arrow = new cc.Node().addComponent(cc.Sprite);
			this.assetImpl.spriteAtlasFrame(arrow, Res.texture.views.homeUI, finish ? "point_1" : "point_2");
			arrow.node.position = points[i];
			arrow.node.opacity = 0;
			arrow.node.parent = this.arrowNode;
			pointsNode.push(arrow.node);
		}
		return pointsNode;
	}

	/** 初始化展示动画 */
	protected _onDisplayShowLevelItems(isAction: boolean, index: number = 0) {
		if (!(index < this.lvNode.children.length)) return;
		let lvItem = this.lvNode.children[index].getComponent(LevelItem);
		lvItem.startInit(isAction, () => this._onDisplayShowLevelItems(isAction, index + 1));
	}

}